﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _7_6 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            dataBind();
        }
        
    }

    /// <summary>
    /// 数据绑定方法
    /// </summary>
    protected void dataBind()
    {
        MyPetShopDataContext db = new MyPetShopDataContext();
        var resule = from p in db.Product
                     select new
                     {
                         p.Name,
                         p.ListPrice,
                         p.Descn,
                         p.Qty,
                         p.ProductId
                     };
        GVData.DataSource = resule;
        GVData.DataBind();
    }

    /// <summary>
    /// 删除数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void GVData_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int index = Convert.ToInt32(GVData.Rows[e.RowIndex].Cells[0].Text);
        MyPetShopDataContext db = new MyPetShopDataContext();
        
        var result = from p in db.Product
                     where p.ProductId == index
                     select p;
        try
        {
            db.Product.DeleteAllOnSubmit(result);
            db.SubmitChanges();
            dataBind();
        }
        catch (Exception)
        {
            Response.Write("<script>alert('删除失败，请重试')</script>");
            throw;
        }
    }

    protected void GVData_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GVData.EditIndex = e.NewEditIndex;
        dataBind();
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GVData_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int productId = (int)GVData.DataKeys[e.RowIndex].Value;
        string name = ((TextBox)(GVData.Rows[e.RowIndex].Cells[1].Controls[0])).Text;
        decimal listPrice = Decimal.Parse(((TextBox)GVData.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
        string descn = ((TextBox)GVData.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        int qty = int.Parse(((TextBox)GVData.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
        
        MyPetShopDataContext db = new MyPetShopDataContext();
        var result = from p in db.Product
                     where p.ProductId == productId
                     select p;
        if (result != null)
        {
            foreach (Product item in result)
            {
                item.Name = name;
                item.ListPrice = listPrice;
                item.Descn = descn;
                item.Qty = qty;

            }
        }
        db.SubmitChanges();
        GVData.EditIndex = -1;
        dataBind();
    }

    protected void GVData_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        //取消编辑状态
        GVData.EditIndex = -1;
        dataBind();
    }

    protected void BtnAdd_Click(object sender, EventArgs e)
    {
        //跳转到添加页面
        Response.Redirect("~/7.6add.aspx");
    }
}